# encoding: utf-8

# datetime模块常用方法
import datetime

# 返回当前的时间对象
now = datetime.datetime.now()
print(now)  # 2021-09-15 15:40:55.127518

# 转换为时间元组(用于计算)
timetuple = now.timetuple()
# time.struct_time(tm_year=2021, tm_mon=9, tm_mday=15, tm_hour=15, tm_min=46, tm_sec=57, tm_wday=2, tm_yday=258, tm_isdst=-1)
print(timetuple)

# 将时间戳转换为时间对象
fromtimestamp = datetime.datetime.fromtimestamp(20200725)
print(fromtimestamp)  # 1970-08-23 03:18:45

# 时间运算

# 例：加3天5小时
day_after = now + datetime.timedelta(days=3,hours=5)
print(day_after)  # 2021-09-18 20:53:12.693715

# 时间替换

# 例：将当前时间替换为2012年
now_replace = now.replace(year=2012)
print(now_replace)  # 2012-09-15 15:54:51.753097
